/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package jmmos.gameserver.model.actor.instance;

import jmmos.gameserver.ai.MMOCharacterAI;
import jmmos.gameserver.ai.MMONpcWalkerAI;
import jmmos.gameserver.model.actor.MMOCharacter;
import jmmos.gameserver.model.actor.MMONpc;
import jmmos.gameserver.model.actor.templates.MMONpcTemplate;
import jmmos.gameserver.model.skills.MMOSkill;
import jmmos.gameserver.network.NpcStringId;
import jmmos.gameserver.network.clientpackets.Say2;
import jmmos.gameserver.network.serverpackets.NpcSay;
import jmmos.gameserver.util.Broadcast;

/**
 * This class manages some npcs can walk in the city.<br>
 * It inherits all methods from MMONpcInstance.
 * @author Rayan
 */
public class MMONpcWalkerInstance extends MMONpc
{
	/**
	 * Constructor of MMONpcWalkerInstance (use MMOCharacter and MMONpcInstance constructor).
	 * @param objectId given object id
	 * @param template MMONpcTemplateForThisAi
	 */
	public MMONpcWalkerInstance(int objectId, MMONpcTemplate template)
	{
		super(objectId, template);
		setInstanceType(InstanceType.MMONpcWalkerInstance);
		setAI(new MMONpcWalkerAI(new MMONpcWalkerAIAccessor()));
	}
	
	/**
	 * AI can't be detached, npc must move always with the same AI instance.
	 * @param newAI AI to set for this MMONpcWalkerInstance
	 */
	@Override
	public void setAI(MMOCharacterAI newAI)
	{
		if (!(_ai instanceof MMONpcWalkerAI))
		{
			_ai = newAI;
		}
	}
	
	@Override
	public void onSpawn()
	{
		getAI().setHomeX(getX());
		getAI().setHomeY(getY());
		getAI().setHomeZ(getZ());
	}
	
	/**
	 * Sends a chat to all _knowObjects
	 * @param chat message to say
	 * @param npcString
	 */
	public void broadcastChat(String chat, NpcStringId npcString)
	{
		NpcSay cs;
		if (npcString == null)
		{
			cs = new NpcSay(getObjectId(), Say2.NPC_ALL, getNpcId(), chat);
		}
		else
		{
			cs = new NpcSay(getObjectId(), Say2.NPC_ALL, getNpcId(), npcString);
		}
		Broadcast.toKnownPlayers(this, cs);
	}
	
	/**
	 * NPCs are immortal
	 * @param i ignore it
	 * @param attacker ignore it
	 * @param awake ignore it
	 */
	@Override
	public void reduceCurrentHp(double i, MMOCharacter attacker, boolean awake, boolean isDOT, MMOSkill skill)
	{
	}
	
	/**
	 * NPCs are immortal
	 * @param killer ignore it
	 * @return false
	 */
	@Override
	public boolean doDie(MMOCharacter killer)
	{
		return false;
	}
	
	@Override
	public MMONpcWalkerAI getAI()
	{
		return (MMONpcWalkerAI) _ai;
	}
	
	protected class MMONpcWalkerAIAccessor extends MMOCharacter.AIAccessor
	{
		/**
		 * AI can't be detached.
		 */
		@Override
		public void detachAI()
		{
		}
	}
	
	@Override
	public boolean isWalker()
	{
		return true;
	}
}
